package cc.picc;

import java.util.Optional;

import cc.picc.quota.QuotaBucketContainer;
import cc.picc.quota.QuotaBucketOperator;
import cc.picc.quota.StringQuotaBucket;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 模拟令牌消费者
 * 
 * @author lijinting01
 *
 */
@Slf4j
@AllArgsConstructor
public class QuotaConsumer implements QuotaBucketOperator {

	private QuotaBucketContainer<StringQuotaBucket, String> quotaBucketContainer;

	@Override
	public void operate(String name) {
		quotaBucketContainer.get(name, (final StringQuotaBucket b) -> {
			Optional<String> optional = b.pop();
			if (!optional.isPresent()) {
				log.info(String.format("没有可用的令牌：当前桶大小是%d", b.size()));
			}
			optional.ifPresent((String s) -> log.info(String.format("当前桶大小是%d   获得的quota编号是：%s", b.size(), s)));
		});
	}
}
